package cn.iocoder.yudao.module.nmkj.service.video;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.nmkj.controller.admin.video.vo.*;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.video.VideoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.nmkj.dal.mysql.video.VideoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.VIDEO_NOT_EXISTS;

/**
 * 展示视频 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class VideoServiceImpl implements VideoService {

    @Resource
    private VideoMapper videoMapper;

    @Override
    public Long createVideo(VideoSaveReqVO createReqVO) {
        // 插入
        VideoDO video = BeanUtils.toBean(createReqVO, VideoDO.class);
        videoMapper.insert(video);
        // 返回
        return video.getId();
    }

    @Override
    public void updateVideo(VideoSaveReqVO updateReqVO) {
        // 校验存在
        validateVideoExists(updateReqVO.getId());
        // 更新
        VideoDO updateObj = BeanUtils.toBean(updateReqVO, VideoDO.class);
        videoMapper.updateById(updateObj);
    }

    @Override
    public void deleteVideo(Long id) {
        // 校验存在
        validateVideoExists(id);
        // 删除
        videoMapper.deleteById(id);
    }

    private void validateVideoExists(Long id) {
        if (videoMapper.selectById(id) == null) {
            throw exception(VIDEO_NOT_EXISTS);
        }
    }

    @Override
    public VideoDO getVideo(Long id) {
        return videoMapper.selectById(id);
    }

    @Override
    public PageResult<VideoDO> getVideoPage(VideoPageReqVO pageReqVO) {
        return videoMapper.selectPage(pageReqVO);
    }

}